package com.itany.shop.dao.impl;

import com.itany.shop.dao.OrderDao;
import com.itany.shop.entity.Item;
import com.itany.shop.entity.Order;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.DataAccessException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.rowmapper.OrderRowMapper;
import com.itany.shop.util.JDBCTemplate;
import com.itany.shop.util.JDBCUtil;
import com.itany.shop.util.KeyHolder;
import com.itany.shop.util.PreparedStatementCreator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author MissChen
 * @date 2024年04月02日 9:08
 */
public class OrderDaoImpl implements OrderDao {
    private JDBCTemplate jdbcTemplate= (JDBCTemplate) ObjectFactory.getObject("jdbc");
    @Override
    public Integer insertReturnPrimaryKey(Order order) {
        KeyHolder keyHolder=new KeyHolder();
        jdbcTemplate.insertReturnPrimaryKey(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
                String sql=new StringBuffer()
                        .append(" insert into ")
                        .append(" t_order ")
                        .append(" (no,price,user_id) ")
                        .append(" values ")
                        .append(" (?,?,?) ")
                        .toString();

                //预编译sql语句，并返回状态集
                PreparedStatement ps=conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
                //设置参数
                ps.setString(1,order.getNo());
                ps.setDouble(2,order.getPrice());
                ps.setInt(3,order.getUser().getId());
                return ps;
            }
        },keyHolder);


        return keyHolder.getKey();
    }

    @Override
    public List<Order> selectByUserId(Integer id) {
        String sql=new StringBuffer()
                .append(" select id,no,price,user_id  ")
                .append(" from t_order ")
                .append(" where user_id=? ")
                .toString();
        List<Order> orders=jdbcTemplate.query(sql,new OrderRowMapper(),id);
        return orders;
    }

    @Override
    public Order selectById(Integer id) {

        String sql=new StringBuffer()
                .append(" select o.id,o.no,o.price,u.username,u.phone,p.name,i.num,i.price,(select count(1) from t_item where order_id=o.id) count ")
                .append(" from t_order o left join t_user u   ")
                .append("  on o.user_id=u.id ")
                .append(" left join t_item i ")
                .append(" on o.id=i.order_id ")
                .append(" left join t_product p ")
                .append(" on i.product_id=p.id  ")
                .append(" where o.id=? ")
                .toString();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        Order order=null;
        try {
            conn=JDBCUtil.getConnection();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs=ps.executeQuery();
            while(rs.next()){
                if(order==null){
                    order=new Order();
                    order.setId(rs.getInt("o.id"));
                    order.setNo(rs.getString("o.no"));
                    order.setPrice(rs.getDouble("o.price"));
                    order.setCount(rs.getInt("count"));
                    User user=new User();
                    user.setUsername(rs.getString("u.username"));
                    user.setPhone(rs.getString("u.phone"));
                    order.setUser(user);
                }
                Item item=new Item();
                item.setPrice(rs.getDouble("i.price"));
                item.setNum(rs.getInt("i.num"));

                Product product=new Product();
                product.setName(rs.getString("p.name"));
                item.setProduct(product);
                //将多条明细添加到订单中取
                order.addItem(item);

            }

            return order;
        } catch (SQLException e) {
           throw new DataAccessException("数据库访问异常");
        }

    }
}
